﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;

namespace ProyectoAlmacen
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void CRUDTest1()
        {
            try
            {

                //PRUEBA DEL METODO HTTP POST
                string postdata = "{\"idMaterial\":\"99\",\"Descripcion\":\"Material99\",\"Estado\":\"1\",\"idUsuario\":\"1\"}";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest
                    .Create("http://localhost:64660/Materiales.svc/Materiales");
                req.Method = "POST";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                string materialJson = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                Material materialCreado = js.Deserialize<Material>(materialJson);
                Assert.AreEqual("99", materialCreado.idMaterial.ToString());
                Assert.AreEqual("Material99", materialCreado.Descripcion);

            }
            catch (WebException e)
            {
                HttpWebResponse resError = (HttpWebResponse)e.Response;
                StreamReader readerError = new StreamReader(resError.GetResponseStream());
                string mensajeError = readerError.ReadToEnd();
                JavaScriptSerializer jsError = new JavaScriptSerializer();
                Error mensajeObtenido = jsError.Deserialize<Error>(mensajeError);
                Assert.AreEqual("Ha ingresado datos repetidos, por favor verifique.", mensajeObtenido.Mensaje);
                        
            }
            
            
        }
        [TestMethod]
        public void CRUDTest2()
        {
            //PRUEBA DEL METODO HTTP GET
            HttpWebRequest req2 = (HttpWebRequest)WebRequest
                .Create("http://localhost:64660/Materiales.svc/Materiales/99");
            req2.Method = "GET";
            HttpWebResponse res2 = (HttpWebResponse)req2.GetResponse();
            StreamReader reader2 = new StreamReader(res2.GetResponseStream());
            string materialJson2 = reader2.ReadToEnd();
            JavaScriptSerializer js2 = new JavaScriptSerializer();
            Material materialObtenido = js2.Deserialize<Material>(materialJson2);
            Assert.AreEqual("99", materialObtenido.idMaterial.ToString());
            Assert.AreEqual("Material99", materialObtenido.Descripcion);
        }

        [TestMethod]
        public void CRUDTest3()
        {

            //PRUEBA DEL METODO HTTP PUT

            string postdata3 = "{\"idMaterial\":\"99\",\"Descripcion\":\"Material99999\",\"Estado\":\"1\",\"idUsuario\":\"1\"}";
            byte[] data3 = Encoding.UTF8.GetBytes(postdata3);
            HttpWebRequest req3 = (HttpWebRequest)WebRequest
                .Create("http://localhost:64660/Materiales.svc/Materiales");
            req3.Method = "PUT";
            req3.ContentLength = data3.Length;
            req3.ContentType = "application/json";
            var reqStream3 = req3.GetRequestStream();
            reqStream3.Write(data3, 0, data3.Length);
            var res3 = (HttpWebResponse)req3.GetResponse();
            StreamReader reader3 = new StreamReader(res3.GetResponseStream());
            string materialJson3 = reader3.ReadToEnd();
            JavaScriptSerializer js3 = new JavaScriptSerializer();
            Material materialaActualizado = js3.Deserialize<Material>(materialJson3);
            Assert.AreEqual("99", materialaActualizado.idMaterial.ToString());
            Assert.AreEqual("Material99999", materialaActualizado.Descripcion);

        }


        [TestMethod]
        public void CRUDTest4()
        {

            //PRUEBA DEL METODO HTTP DELETE

            HttpWebRequest req4 = (HttpWebRequest)WebRequest
                .Create("http://localhost:64660/Materiales.svc/Materiales/99");
            req4.Method = "DELETE";
            HttpWebResponse res4 = (HttpWebResponse)req4.GetResponse();
            StreamReader reader4 = new StreamReader(res4.GetResponseStream());
            string materialJson4 = reader4.ReadToEnd();
            JavaScriptSerializer js4 = new JavaScriptSerializer();
            Material materialObtenido = js4.Deserialize<Material>(materialJson4);
            HttpWebRequest req5 = (HttpWebRequest)WebRequest
                .Create("http://localhost:64660/Materiales.svc/Materiales/99");
            req5.Method = "GET";
            HttpWebResponse res5 = (HttpWebResponse)req5.GetResponse();
            StreamReader reader5 = new StreamReader(res5.GetResponseStream());
            string materialJson5 = reader5.ReadToEnd();
            JavaScriptSerializer js5 = new JavaScriptSerializer();
            Material materialObtenido5 = js5.Deserialize<Material>(materialJson5);
            Assert.AreEqual(null, materialObtenido5);

        }
    }
}
